<?php 

namespace php_frame;

class php_frame{
	static public $classMap = array();
	public $assign;

	static public function run(){
		p('run_start');
		\core\lib\log::init();
		\core\lib\log::log($_SERVER,'log');
		$route = new \core\lib\route();
		$action = $route->action;
		$ctrlClass = $route->ctrl;
		$ctrlfile = app.'/ctrl/'.$ctrlClass.'Ctrl.php';
		$ctrlClass = '\\'.module.'\ctrl\\'.$ctrlClass.'Ctrl';

		if(is_file($ctrlfile)){
			//自动加载和手动加载
			// include $ctrlfile;
			$ctrl = new $ctrlClass;
			// p(get_class_methods($ctrl));
			$ctrl->$action();
		}else{
			throw new \Exception('找不到控制器'.$ctrlClass);
		}
	}

	static public function load($class){
		//自动加载类库
		if(isset($classMap[$class])){
			return true;
		}
		$class = str_replace('\\','/',$class);
		// p($class);
		$file = php_frame.'/'.$class.'.php';
		// p($file);
		if(is_file($file)){
			include $file;
			self::$classMap[$class] = $class;
		}else{
			return false;
		}	
	}

	public function assign($name,$value){
		$this->assign[$name] = $value;
	}

	public function display($file){
		// $file = app.'/views/'.$file;
		// // p($file);
		// if(is_file($file)){
		// 	extract($this->assign);
		// 	include $file;
		// }

		$file = app.'/views/'.$file;
		p($file);
		p($this->assign);
		if(is_file($file)){
			// extract($this->assign);
			$loader = new \Twig_Loader_Filesystem(app.'/views');
			$twig = new \Twig_Environment($loader,array(
				 // 'cache' => './compilation_cache',
				));
			$template = $twig->loadTemplate('index.html');
			$template->display($this->assign?$this->assign:'');

			// $result =  $twig->render($file, $this->assign);
			// p($result);
		}

	}
}
